package com.ruoyi.custservice.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.base.yaml.BusinessConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.client.RestTemplate;

/**
 * 借款申请Controller
 *
 * @author african-loan
 * @date 2021-12-30
 */
@RestController
@RequestMapping("/business/LoanOrderRepay")
public class LoanOrderRepayController extends BaseController {
    @Autowired
    private ILoanOrderService loanOrderService;
    @Autowired
    BusinessConfig businessConfig;
    private static RestTemplate restTemplate;
    static {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(180000);
        factory.setReadTimeout(180000);
        restTemplate = new RestTemplate(factory);
    }

/**
 * 查询借款申请列表
 */
@PreAuthorize("@ss.hasPermi('business:LoanOrder:list')")
@GetMapping("/list")
        public TableDataInfo list(LoanOrder loanOrder) {
        startPage();
        List<LoanOrder> list = loanOrderService.selectLoanOrderList(loanOrder);
        return getDataTable(list);
    }

    /**
     * 触发重新放款
     */
    @PreAuthorize("@ss.hasPermi('business:LoanOrder:edit')")
    @Log(title = "重新放款", businessType = BusinessType.UPDATE)
    @PostMapping("/Repay/{ids}")
    public AjaxResult repay(@PathVariable Long[] ids) {
        String url = businessConfig.getApiurl()+"/appTocustServer/callInitiateTransfer?ids="+ StringUtils.join(ids,",");
        logger.info("重新放款调用业务URL:",url);
        JSONObject res = restTemplate.getForObject(url,JSONObject.class);
        return AjaxResult.success(res.get("msg").toString());
    }

}


